package com;

import com.pc.admin.entity.Person;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @Author : ZJ
 * @create 2023/3/23 13:09
 */
public class DaDatest {


    public static void main(String[] args) {

    }


    @Test
    public void test1() {
        nihao("zhangJing", (m) -> System.out.println("你好" + m));
    }

    public void nihao(String name, Consumer<String> con) {
        con.accept(name);
    }

    @Test
    public void test2() {
        List<Integer> list = getaNumList(10, () -> (int) (Math.random() * 100));
        for (Integer integer : list) {
            System.out.println(integer);
        }
    }

    public List<Integer> getaNumList(Integer num, Supplier<Integer> supplier) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Integer s = supplier.get();
            list.add(s);
        }
        return list;
    }

    @Test
    //Predicate<T>
    public void test3() {
        List<String> list = Arrays.asList("kzf", "dad", "sdada", "hwidh");
        List<String> strings = filterStr(list, (s -> s.length() > 3));
        for (String a : strings) {
            System.out.print(a + " ");
        }
    }

    //将满足条件的字符串，放入集合中
    public List<String> filterStr(List<String> list, Predicate<String> pre) {
        List<String> list1 = new ArrayList<>();
        for (String a : list) {
            if (pre.test(a)) {
                list1.add(a);
            }
        }
        return list1;
    }


    @Test
    public void test4() {
        ArrayList<String> list = new ArrayList<>();
        Stream<String> stream = list.stream();

        //2.通过Stream类中的静态方法of（）
        Stream<String> stringStream = Stream.of("aaa", "bbb", "ccc");

        //4.创建无限流
        Stream<Integer> iterate = Stream.iterate(0, (x) -> x + 2);
        iterate.limit(10).forEach(System.out::println);

        //生成
        Stream.generate(() -> Math.random() * 100).limit(10).forEach(System.out::println);

    }



    List<Person> personList = Arrays.asList(
            new Person("张三", 13),
            new Person("李四", 28),
            new Person("王麻子", 38),
            new Person("张三", 48),
            new Person("张三", 48),
            new Person("张三", 48)
            );



@Test
    public void test5() {
    Stream<Person> stream = personList.stream().filter((s) -> s.getAge() > 30);
    stream.forEach(System.out::println);
}

    @Test
    public void test6() {
        personList.stream()
                .filter((e) -> e.getAge() > 30)
                .limit(2)
                .skip(1)
                .distinct()
                .forEach(System.out::println);
    }

}
